<?php
/**
 * 商户管理
 * User: hls
 * Date: 2016/3/21
 * Time: 10:10
 */

namespace Shop\Controller;
use Common\Model\CouponModel;
use Shop\BaseController;
use Think\Page;

class RestaurantController extends BaseController
{
    public function search(){

    }
    /**
     * storeList 门店列表
     * 只显示自己的门店
     */
    public function storeList(){
        $store = M('s_store',null,DB_MAIN_CFG);
        $verification = M('s_verification',null,DB_MAIN_CFG);
        $restaurantId = session('uid');

        //分页
        $count = $store->count();
        $Page = new Page($count, 10);
        //分页
        fetchPage($this->smarty, $Page);
        $list = $store
            ->field('s_restaurant.restaurantName,s_store.storeName,s_store.id,s_store.address,s_store.status')
            ->join('s_restaurant on s_restaurant.id = s_store.restaurantId')
            ->where('s_store.restaurantId = '.$restaurantId)
            ->order('s_store.updateTime desc')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->smarty->assign('list', $list);
        $this->smarty->display('Business/store_list.html');
    }
    /**
     * verficationList 核销员列表
     */
    public function verificationList(){

        $verification = M('s_verification',null,DB_MAIN_CFG);
        $restaurantId = session('restaurantId');

        //分页
        $count = $verification->count();
        $Page = new Page($count, 10);
        //分页
        fetchPage($this->smarty, $Page);
        //因为要输出商户名字和门店名字  3表联合查询
        $list = $verification
            ->field('s_verification.id,s_verification.verificationName,s_verification.verifyNum,
            s_verification.jobNum,s_verification.phone,s_verification.status,
            s_store.storeName,s_restaurant.restaurantName')
            ->join('s_store on s_verification.storeId = s_store.id')
            ->join('s_restaurant on s_restaurant.id = s_verification.restaurantId')
            ->where('s_verification.restaurantId = '.$restaurantId)
            ->limit($Page->firstRow , $Page->listRows)
            ->select();
        $this->smarty->assign('list', $list);
        $this->smarty->display('Business/verification_list.html');
    }
    /**
     *cancel 卡券核销
     */
    public function consume(){
        $this->smarty->display('Business/consume.html');
    }

    /**
     * searchCard
     * ajax 接口 查找卡券
     */
    public function searchCard(){
        $verification = M('s_verification',null,DB_MAIN_CFG);
        //username为工号
        $jobNum = session('jobNum');
        $restaurantId = $verification->where("jobNum='{$jobNum}'")->getField('restaurantId');

        $code = I('post.code',null);
        $password = I('post.password',null);
        $card_code = M('card_code',null,DB_MAIN_CFG);
        $card_base = M('card_base_info',null,DB_MAIN_CFG);
        $restaurant = M('s_restaurant',null,DB_MAIN_CFG);
        if(empty($password)){
            $card_list_item = $card_code->where("code='{$code}' and restaurantId = '{$restaurantId}'")->find();
        }else{
            $card_list_item = $card_code->where("code='{$code}' and restaurantId = '{$restaurantId}' and password ='{$password}'")->find();
        }
        //判断是否存在该卡券
        if(empty($card_list_item)){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '不存在该卡券';
            $this->ajaxReturn($data);
        }
        //判断该卡券是否被领用
        if(empty($card_list_item['uid'])){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '该卡券还没有被领取';
            $this->ajaxReturn($data);
        }
        //判断该卡券是否已经被使用
        if($card_list_item['status'] == CouponModel::CODE_STATUS_USED){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '该卡券已经被使用';
            $this->ajaxReturn($data);
        }
        //判断该卡券是否上线
        if($card_list_item['status'] == CouponModel::CODE_STATUS_USED){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '该卡券已经被使用';
            $this->ajaxReturn($data);
        }
        //获取该卡券的有效期
//        if(empty($card_list_item['beginTimestamp'])){
//            $beginTime = $card_list_item['receiveTimestamp'] + $card_list_item['fixedBeginTerm']*24*60*60;
//            $endTime = $card_list_item['receiveTimestamp'] + $card_list_item['fixedBeginTerm']*24*60*60+$card_list_item['fixedTerm']*24*60*60;
//        }else{
        $beginTime = $card_list_item['beginTimestamp'];
        $endTime = $card_list_item['endTimestamp'];
//        }
        if($endTime < time()){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '卡券已过期';
            $this->ajaxReturn($data);
        }
        $card_info_item = $card_base->find($card_list_item['cardId']);
        $restaurantName = $restaurant->where('id='.$card_info_item['restaurantId'])->getField('restaurantName');
        $data['status'] = AJAX_SUCCESS;
        $card_type = CouponModel::getCardType();
        $data['data'] = array(
            'restaurantName' => $restaurantName,
            'cardType' => $card_type[$card_info_item['type']]['title'],
            'code' => $code,
            'beginTime' => date('Y-m-d',$beginTime),
            'endTime' => date('Y-m-d',$endTime),
        );
        $this->ajaxReturn($data);

    }
    /**
     * ajaxCancel
     * ajax接口  核销卡券
     */
    public function ajaxConsume(){
        $jobNum      = session('jobNum');
        $consume_log    = M('consume_log',null,DB_MAIN_CFG);
        $card_code      = M('card_code',null,DB_MAIN_CFG);
        $verification   = M('s_verification',null,DB_MAIN_CFG);
        $card_info      = M('card_base_info',null,DB_MAIN_CFG);
        $store          = M('s_store',null,DB_MAIN_CFG);
        $restaurant     = M('s_restaurant',null,DB_MAIN_CFG);
        $code           = I('post.code'.null);
        $password       = I('post.password',null);

        $time = getdate(time());
        $day = $time['wday'];

        $restaurantId = $verification->where('jobNum='.$jobNum)->getField('restaurantId');
        $storeId = $verification->where('jobNum='.$jobNum)->getField('storeId');

        $restaurantName = $restaurant->where('id='.$restaurantId)->getField('restaurantName');
        $storeName = $store->where('id='.$storeId)->getField('storeName');
        if(empty($password)){
            $card_list_item = $card_code->where("code='{$code}' and restaurantId = '{$restaurantId}'")->find();
        }else{
            $card_list_item = $card_code->where("code='{$code}' and restaurantId = '{$restaurantId}' and password ='{$password}'")->find();
        }
        //判断是否存在该卡券
        if(empty($card_list_item)){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '不存在该卡券';
            $this->ajaxReturn($data);
        }
        //判断该卡券是否被领用
        if(empty($card_list_item['uid'])){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '该卡券还没有被领取';
            $this->ajaxReturn($data);
        }
        //判断该卡券是否已经被使用
        if($card_list_item['status'] == CouponModel::CODE_STATUS_USED){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '该卡券已经被使用';
            $this->ajaxReturn($data);
        }
        //判断该卡券是否上线
        if($card_list_item['status'] == CouponModel::CODE_STATUS_USED){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '该卡券已经被使用';
            $this->ajaxReturn($data);
        }
        //获取该卡券的有效期
//        if(empty($card_list_item['beginTimestamp'])){
//            $endTime = $card_list_item['receiveTimestamp'] + $card_list_item['fixedBeginTerm']*24*60*60+$card_list_item['fixedTerm']*24*60*60;
//        }else{
//            $endTime = $card_list_item['endTimestamp'];
//        }
        if($card_list_item['endTimestamp'] < time()){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '卡券已过期';
            $this->ajaxReturn($data);
        }
        $card_info_item = $card_info->where('id='.$card_list_item['cardId'].' and  FIND_IN_SET('.$day.',availableDay) ')->find();
        if(empty($card_info_item)){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '该日无法使用该卡券';
            $this->ajaxReturn($data);
        }
        $card_storeId = explode(',',$card_info_item['storeId']);
        if(!in_array($storeId,$card_storeId)){
            $data['status'] = AJAX_FASLE;
            $data['msg'] = '该门店无法核销该卡券';
            $this->ajaxReturn($data);
        }

        $consume_log_data = array(
            'code' => $code,
            'consumeTime'=>time(),
            'consumeFrom' =>CouponModel::CARD_CONSUME_FROM_ADMIN,
            'jobNum' => $jobNum,
            'restaurantId' => $restaurantId,
            'storeId' => $storeId,
            'restaurantName'=>$restaurantName,
            'storeName' => $storeName,
            'cardTitle'=> $card_info_item['title'],
            'cardType' => $card_info_item['type'],
        );
        //添加核销记录
        $consume_log->add($consume_log_data);
        //更改卡券的使用状态
        $card_code->data('status='.CouponModel::CODE_STATUS_USED)->where('code='.$code)->save();
        $verification->setInc('verifyNum');
        $data['status'] = AJAX_SUCCESS;
        $data['msg'] = '核销成功';
        $this->ajaxReturn($data);

    }
}